//
//  ZDStatusCell.m
//  OCSinaWeiBo
//
//  Created by Apple on 15/11/2.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import "ZDStatusCell.h"
#import "ZDStatusTopView.h"
#import "UILabel+Extension.h"
#import "ZDStatusBottomView.h"
#import "ZDPictureView.h"



@interface ZDStatusCell ()

@property (nonatomic, strong) ZDStatusTopView *topView;



@end

@implementation ZDStatusCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        [self prepareUI];
    }
    
    return self;
}


//准备子控件
- (void)prepareUI
{
    //添加子控件
    [self.contentView addSubview:self.topView];
    [self.contentView addSubview:self.contentLabel];
    [self.contentView addSubview:self.pictureView];
    [self.contentView addSubview:self.bottomView];
    
    //添加约束
    [_topView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.right.mas_equalTo(self.contentView);
        make.height.mas_equalTo(54);
    }];
    
    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.contentView.mas_left).offset(margin);
//        make.right.mas_equalTo(self.contentView.mas_right).offset(-margin);   //如果设置左右约束，那么label的宽度将随屏幕的宽度而改变，在计算cell的高度时，拿到的label的frame不是最终的frame(宽度为320而非375)，那么当最终显示时，由于宽度变成375，那么label一行可以显示更多的文字，根据sizeToFit，其高度势必会减小。与当初参与计算的高度是由偏差的
        make.width.mas_equalTo(ZDScreenSize.width - 2 * margin);
        make.top.mas_equalTo(_topView.mas_bottom).offset(margin);
    }];
    
//    [_pictureView mas_makeConstraints:^(MASConstraintMaker *make) {
//        
//        make.top.mas_equalTo(_contentLabel.mas_bottom).offset(margin);
//        make.left.mas_equalTo(self).offset(margin);
//        make.width.height.mas_equalTo(0);
//    }];
    
//     ZDLog(@"%@", _pictureView.constraints);
//    self.pictureViewWidthC = _pictureView.constraints.firstObject;
//    self.pictureViewHeightC = _pictureView.constraints.lastObject;
    
    [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.mas_equalTo(self.contentView);
        make.top.mas_equalTo(_pictureView.mas_bottom).offset(margin);
        make.height.mas_equalTo(44);
        
//        make.bottom.mas_equalTo(self.contentView);
    }];
}


- (void)setStatus:(ZDStatus *)status
{
    _status = status;
    
    self.topView.status = status;
    
    self.contentLabel.text = status.text;
    
    self.pictureView.status = status;
    
    CGSize size = [self.pictureView calculatCollectionViewSize];
    
    //为约束赋值
    self.pictureViewWidthC.constant = size.width;
    self.pictureViewHeightC.constant = size.height;
    
//    ZDLog(@"%@", NSStringFromCGSize(size));
}

/**
 *  返回cell的高度
 */
- (CGFloat)rowHeight:(ZDStatus *)status
{
//    _status = status;    不能写_status! 因为需要调用status的set方法 来设置子控件的内容，这样才能根据子控件的约束来计算cell自身的高度
    
    self.status = status;
    
    [self layoutIfNeeded];
    
//    ZDLog(@"%@", NSStringFromCGRect(self.bottomView.frame));
    
    return CGRectGetMaxY(self.bottomView.frame);
}


#pragma mark 懒加载
- (ZDStatusTopView *)topView
{
    if (!_topView)
    {
        _topView = [[ZDStatusTopView alloc] init];
    }
    
    return _topView;
}

- (UILabel *)contentLabel
{
    if (!_contentLabel)
    {
        _contentLabel = [[UILabel alloc] initWithFontSize:14 color:nil];
        
        _contentLabel.numberOfLines = 0;
        
//        _contentLabel.backgroundColor = [UIColor greenColor];
    }
    
    return _contentLabel;
}

- (ZDPictureView *)pictureView
{
    if (!_pictureView)
    {
        _pictureView = [[ZDPictureView alloc] init];
        
//        _pictureView.backgroundColor = [UIColor yellowColor];
    }
    
    return _pictureView;
}

- (ZDStatusBottomView *)bottomView
{
    if (!_bottomView)
    {
        _bottomView = [[ZDStatusBottomView alloc] init];
    }
    
    return _bottomView;
}

@end
